Vue.component("carousel", {
  template:
    "<div :style='swiperStyle' v-if='module1.length'><div class='swiper-container lw'><div class='swiper-wrapper'><div class='swiper-slide' v-for='item in module1'><img :src='item.bannerUrl' alt=''></div></div><div class='swiper-pagination lw'></div></div></div>",
  data: function () {
    return {
      module1: "",
      swiper: null,
      swiperStyle: {
        padding: "15px 15px 0 15px",
        backgroundImage: this.notbg ? "" : "url(../../image/ui/bg_4.png)",
        backgroundSize: "100% 132px",
        backgroundPositionY: "-1px",
        backgroundRepeat: "no-repeat",
        marginBottom: this.bottom + "px",
      },
    };
  },
  props: ["type", "timestamp", "bottom", "notbg"],
  created: function () {
    this.getBannerData();
  },
  watch: {
    timestamp: function (val) {
      this.getBannerData();
    },
    type: function (val) {
      if (!this.type != val) {
        this.getBannerData();
      }
    },
  },
  methods: {
    getBannerData: function () {
      this.module1 = [];
      var self = this;
      var params = {
        bannerPositionId: self.type,
      };
      var url = "bannerConfig/findBannerList";
      $S.POST(url, params, function (ret) {
        if (ret.status == 200) {
          self.module1 = ret.data.listData;
          self.$nextTick(function () {
            self.initSwiper();
          });
        }
      });
    },
    initSwiper: function () {
      this.swiper && this.swiper.destroy();
      if (this.module1.length) {
        var self = this;
        this.swiper = new Swiper(".swiper-container", {
          loop: true,
          autoheight: true,
          autoplay: {
            autoplay: true,
            disableOnInteraction: false,
          },
          pagination: {
            el: ".swiper-pagination",
          },
          on: {
            click: function () {
              var item = self.module1[this.realIndex];
              self.swiperFn(item);
            },
          },
        });
      }
    },
    swiperFn: function (item) {
      console.log(item.contentSource);
      console.log(item.contextIdValue);
      if (item.contentSource == "1") {
        fnOpenPublicWin(item.bannerTitle, item.outUrl, {
          item: item,
        });
      } else if (item.contentSource == "2") {
        if (!item.contextIdValue) {
          return;
        }
        // 跳转商品分类
        fnBannerGetName1({
          type: 1,
          id: item.contextIdValue,
        });
      } else if (item.contentSource == "3") {
        if (!item.contextIdValue) {
          return;
        }
        // 跳转商品详情
        fnBannerOpenShop({
          type: 2,
          id: item.contextIdValue,
        });
      } else if (item.contentSource == "4") {
        if (!item.contextIdValue) {
          return;
        }
        // 跳转基本营销
        fnBannerGetName2({
          type: 3,
          id: item.contextIdValue,
        });
      } else if (item.contentSource == "5") {
        if (!item.contextIdValue) {
          return;
        }
        // 跳转满赠营销
        fnBannerGetName3({
          type: 4,
          id: item.contextIdValue,
        });
      } else if (item.contentSource == "6") {
        // 跳转app首页
        api.closeToWin({
          name: "main",
        });
        api.setTabBarAttr({
          index: 0,
        });
        api.setFrameGroupAttr({
          name: "tab_index_frms",
          hidden: false,
        });
      } else if (item.contentSource == "7") {
        // 跳转视频tab页
        api.closeToWin({
          name: "main",
        });
        api.setTabBarAttr({
          index: 1,
        });
        api.sendEvent({
          name: "jumpToVideo",
        });
        api.setFrameGroupAttr({
          name: "tab_index_frms",
          hidden: true,
        });
      } else if (item.contentSource == "8") {
        // 跳转视频tab页
        fnOpenPublicWin("文章详情", "teacher/info_detail", {
          messageInfoId: item.contextIdValue,
          msgType: 12,
        });
      } else if (item.contentSource == "9") {
        // 跳转课件详情
        fnOpenPublicWin("课件详情", "video/courseware_detail", {
          kjId: item.contextIdValue,
        });
      } else if (item.contentSource == "10") {
        // 跳转绘本详情
        fnOpenPublicWin("绘本详情", "video/hb_detail", {
          hbId: item.contextIdValue,
        });
      }
    },
  },
});

// params
// type 1 分类 2 商品 3 基本营销 4 满赠营销
// id 业务ID
// name 业务名字
function fnBannerOpenShop(params) {
  const url =
    "https://shop98220230.m.youzan.com/wscshop/showcase/feature?alias=QPOMQr3MUm&kdt_id=98028062";
  api.openApp({
    iosUrl: url,
    uri: url,
  });

//   // 到商城
//   fnOpenWin({
//     name: "shop_mall_win",
//     url: "../tabs/tab_shop.html",
//     slidBackEnabled: false,
//     reload: false,
//     animation: {
//       type: "movein",
//       subType: "from_bottom",
//       duration: 500,
//     },
//     pageParam: {
//       bannerParams: params,
//     },
//   });
//   api.sendEvent({
//     name: "fnBannerOpenShop",
//     extra: {
//       bannerParams: params,
//     },
//   });
}

// 满赠
function fnBannerGetName3(param) {
  var url = "common/get";
  var params = {
    entityName: "smsBasicGifts",
    id: param.id,
  };
  var callback = function (data) {
    param.name = data.name;
    fnBannerOpenShop(param);
  };
  POST(url, params, callback);
}

// 基本营销
function fnBannerGetName2(param) {
  var url = "common/get";
  var params = {
    entityName: "smsBasicMarking",
    id: param.id,
  };
  var callback = function (data) {
    param.name = data.name;
    fnBannerOpenShop(param);
  };
  POST(url, params, callback);
}

// 分类
function fnBannerGetName1(param) {
  var url = "common/get";
  var params = {
    entityName: "pmsProductCategory",
    id: param.id,
  };
  var callback = function (data) {
    param.name = data.name;
    fnBannerOpenShop(param);
  };
  POST(url, params, callback);
}
